package com.inspur.resourcequery;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;

import com.inspur.common.BaseActivity;
import com.inspur.common.domain.RequestInfo;
import com.inspur.common.utils.URLManager;
import com.inspur.resourcequery.adapter.PhysicsInfoPagerAdapter;
import com.inspur.resourcequery.model.PhysicsModel;
import com.inspur.resourcequery.model.ResData;

public class PhysicsInfoActivity extends BaseActivity {

	private ResData resData;
	private PhysicsModel physicsModel = new PhysicsModel();

	private ViewPager mViewPager;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_physics_info);

		resData = (ResData) getIntent().getSerializableExtra("data");
		actionBar.setTitle("物理信息");
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
		queryData();

	}

	/**
	 * 查询数据列表
	 */
	protected void queryData() {
		RequestInfo req = new RequestInfo();
//		RequestInfo.method = "GET";
		req.context = PhysicsInfoActivity.this;
		req.requestUrl = URLManager.URL + URLManager.RES_QUERYPHYSICVIEW;
		try {
			JSONObject condition = new JSONObject();
//			condition.put("ResClassName", models.getResclassenname());
//			condition.put("related_ne_id", "121017328");
			condition.put("related_ne_id", resData.getInt_id());
			req.params = condition.toString();
			// req.params="?"+condition.toString();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		super.getDataFromServer(req, new DataCallback<String>() {
			@Override
			public void processData(String paramObject) {
				Log.d("物理信息:", paramObject);
				parseResult(paramObject);
			}

			@Override
			public void getDataFailed() {
				// 这里仅作演示，实际请求失败后怎么操作根据实际情况来看

			}

		}, PhysicsInfoActivity.this);
	}

	public void parseResult(String paramObject) {
		try {
			JSONObject jsonObj = new JSONObject(paramObject);
			physicsModel.setFrameSize(jsonObj.getInt("rack_size"));
			physicsModel.setBasketSize(jsonObj.getInt("shelf_size"));
			physicsModel.setBoardSize(jsonObj.getInt("card_size"));
			physicsModel.setPortSize(jsonObj.getInt("port_size"));
			physicsModel.setFrameModel(jsonObj.getString("resclassname_rack"));
			physicsModel.setBasketModel(jsonObj.getString("resclassname_shelf"));
			physicsModel.setBoardModel(jsonObj.getString("resclassname_card"));
			physicsModel.setPortModel(jsonObj.getString("resclassname_port"));
			physicsModel.setPortFree(jsonObj.getString("attr_port_state_type_0").split(","));
			physicsModel.setPortUse(jsonObj.getString("attr_port_state_type_1").split(","));
			physicsModel.setPortOther(jsonObj.getString("attr_port_state_type_2").split(","));
			
			if(physicsModel.getFrameSize() > 0){
				JSONArray dataArray = jsonObj.getJSONArray("rack_list");
				for(int i = 0; i < dataArray.length(); i++){
					JSONObject resObj = dataArray.getJSONObject(i);
					ResData resData = new ResData();
					resData.setInt_id(resObj.getString("int_id"));
					resData.setName(resObj.getString("zh_label"));
					resData.setCity_id(resObj.has("city_id") ? resObj.getString("city_id") : "");
					resData.setCityName(resObj.has("city_id_transName") ? resObj.getString("city_id_transName") : "");
					resData.setResclassenname(physicsModel.getFrameModel());
					physicsModel.getFrameList().add(resData);
				}
			}
			
			if(physicsModel.getBasketSize() > 0){
				JSONArray dataArray = jsonObj.getJSONArray("shelf_list");
				for(int i = 0; i < dataArray.length(); i++){
					JSONObject resObj = dataArray.getJSONObject(i);
					ResData resData = new ResData();
					resData.setInt_id(resObj.getString("int_id"));
					resData.setName(resObj.getString("zh_label"));
					resData.setCity_id(resObj.has("city_id") ? resObj.getString("city_id") : "");
					resData.setCityName(resObj.has("city_id_transName") ? resObj.getString("city_id_transName") : "");
					resData.setResclassenname(physicsModel.getBasketModel());
					physicsModel.getBasketList().add(resData);
				}
			}
			
			if(physicsModel.getBoardSize() > 0){
				JSONArray dataArray = jsonObj.getJSONArray("card_list");
				for(int i = 0; i < dataArray.length(); i++){
					JSONObject resObj = dataArray.getJSONObject(i);
					ResData resData = new ResData();
					resData.setInt_id(resObj.getString("int_id"));
					resData.setName(resObj.getString("zh_label"));
					resData.setCity_id(resObj.has("city_id") ? resObj.getString("city_id") : "");
					resData.setCityName(resObj.has("city_id_transName") ? resObj.getString("city_id_transName") : "");
					resData.setResclassenname(physicsModel.getBoardModel());
					physicsModel.getBoardList().add(resData);
				}
			}
			
			if(physicsModel.getPortSize() > 0){
				JSONArray dataArray = jsonObj.getJSONArray("port_list");
				for(int i = 0; i < dataArray.length(); i++){
					JSONObject resObj = dataArray.getJSONObject(i);
					ResData resData = new ResData();
					resData.setInt_id(resObj.getString("int_id"));
					resData.setName(resObj.getString("zh_label"));
					resData.setCity_id(resObj.has("city_id") ? resObj.getString("city_id") : "");
					resData.setCityName(resObj.has("city_id_transName") ? resObj.getString("city_id_transName") : "");
					resData.setResclassenname(physicsModel.getPortModel());
					String state = resObj.getString("state");
					
					for(String type : physicsModel.getPortFree()){
						if(state.equals(type)){
							resData.setType("0");
							break;
						}
					}
					if(resData.getType() == null){
						for(String type : physicsModel.getPortUse()){
							if(state.equals(type)){
								resData.setType("1");
								break;
							}
						}
					}
					if(resData.getType() == null){
						for(String type : physicsModel.getPortOther()){
							if(state.equals(type)){
								resData.setType("2");
								break;
							}
						}
					}
					
					physicsModel.getPortList().add(resData);
				}
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		initTab();
	}


	private final TabListener mTabListener = new TabListener() {
		private final static String TAG = "TabListener";

		@Override
		public void onTabReselected(Tab tab, android.app.FragmentTransaction ft) {
			// TODO Auto-generated method stub
			Log.d(TAG, "onTabReselected");
		}

		@Override
		public void onTabSelected(Tab tab, android.app.FragmentTransaction ft) {
			// TODO Auto-generated method stub
			Log.d(TAG, "onTabSelected()");
			if (mViewPager != null)
				mViewPager.setCurrentItem(tab.getPosition());
		}

		@Override
		public void onTabUnselected(Tab tab, android.app.FragmentTransaction ft) {
			// TODO Auto-generated method stub
			Log.d(TAG, "onTabUnselected()");
		}
	};

	class PagerChangeListener implements OnPageChangeListener {
		@Override
		public void onPageScrollStateChanged(int arg0) {
			// TODO Auto-generated method stub
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
			// TODO Auto-generated method stub
		}

		@Override
		public void onPageSelected(int arg0) {
			getActionBar().selectTab(getActionBar().getTabAt(arg0));
		}
	}

	protected void initTab() {

		actionBar.addTab(actionBar.newTab().setText("机架(" + physicsModel.getFrameSize() + ")").setTabListener(mTabListener));
		actionBar.addTab(actionBar.newTab().setText("机框(" + physicsModel.getBasketSize() + ")").setTabListener(mTabListener));
		actionBar.addTab(actionBar.newTab().setText("板卡(" + physicsModel.getBoardSize() + ")").setTabListener(mTabListener));
		actionBar.addTab(actionBar.newTab().setText("端口(" + physicsModel.getPortSize() + ")").setTabListener(mTabListener));

		mViewPager = (ViewPager) findViewById(R.id.vPager);
		getFragmentManager();
		mViewPager.setAdapter(new PhysicsInfoPagerAdapter(getSupportFragmentManager()));
		mViewPager.setOnPageChangeListener(new PagerChangeListener());
		mViewPager.setCurrentItem(0);
	}

	@Override
	protected void loadViewLayout() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void findViewById() {
		// TODO Auto-generated method stub
	}

	@Override
	protected void setListener() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void processLogic() {
		// TODO Auto-generated method stub

	}

	public ResData getResData() {
		return resData;
	}

	public PhysicsModel getPhysicsModel() {
		return physicsModel;
	}
	
	
}
